package 栈;
/*
* 栈是一种数据结构,先进后出,后进先出,经典的应用有逆波兰计算器
* 数组实现思路分析:
* Stack[]: 模拟栈
* top: 栈顶 初始值为-1或者为零(根据个人喜好进行选择)
* capacity: 容量
* */



public class Stack {
    private int capacity;
    private int top = -1;
    private int[] stack = new int[capacity];

    public Stack(int capacity) {
        this.capacity = capacity;
    }

    // 入栈
    public void push(int val){
        if(IsFull()){
            throw new StackFullException("栈满!");
        }
        stack[++top] = val;
    }

    // 出栈
    public int pop(){
        // 判断栈是否为空
        if(IsEmpty()){
            throw new RuntimeException("栈空,无法出栈!");
        }
        int topVal = stack[top];
        top--;
        return topVal;
    }

    // 取栈顶元素
    public int peek(){
        if(IsFull()){
            throw new StackFullException("栈满!");
        }
        return stack[top--];
    }

    public int Peek(){
        if(IsEmpty()){
            throw new RuntimeException("栈空!");
        }
        return stack[top];
    }



    // 判断栈是否空的方法
    public Boolean IsEmpty(){
        return top == -1;
    }


    // 判断是否栈满方法
    public Boolean IsFull(){
        return top == capacity - 1;
    }


}


